PHP

How to demonstrate the Array index out of bound exception using exception handling in PHP | PHP program

How to demonstrate the Array index out of bound exception using exception handling in PHP | PHP program

How to demonstrate the Array index out of bound exception using exception handling in PHP | PHP program

We will use try, catch, and throw keywords to demonstrate the Array index out of bounds exception in a PHP program.

<?php


try
{
    $colors = array(
        "Red",
        "Green",
        "Blue"
    );
    $index = 3;

    if ($index >= count($colors)) throw new Exception("Array index out of bound");

    printf("Array element: %s<br>", $colors[$index]);

}
catch(Exception $e)
{
    printf("Exception: %s", $e->getMessage());
}
?>
TutorialsLink Curator

This is an official account managed by the team TutorialsLink Community